<template>
    <div class="goods-list">
        <!-- 在网页中有两种跳转方式
        1.使用a标签
        2.使用window.location.href -->

        <!-- router-link导航方式 -->
        <!-- <router-link :to="'/home/commodity/shopinfo/'+item.id" tag="div" class="goods-item" v-for="item in shoplist" :key="item.id">
            <img :src="item.img_url" alt="">
            <h1 class="title">{{item.title}}</h1>
            <div class="info">
                <p class="price">
                    <span class="now">￥{{item.sell_price}}</span>
                    <span class="old">￥{{item.market_price}}</span>
                </p>
                <p class="sell">
                    <span>热卖中</span>
                    <span>剩{{item.stock_quantity}}件</span>
                </p>
            </div>
        </router-link> -->


        <!-- 2.使用js的window.location.href的方式  ,-->
        <div @click="goNewurl(item.id)" class="goods-item" v-for="item in shoplist" :key="item.id">
            <img :src="item.img_url" alt="">
            <h1 class="title">{{item.title}}</h1>
            <div class="info">
                <p class="price">
                    <span class="now">￥{{item.sell_price}}</span>
                    <span class="old">￥{{item.market_price}}</span>
                </p>
                <p class="sell">
                    <span>热卖中</span>
                    <span>剩{{item.stock_quantity}}件</span>
                </p>
            </div>
        </div>
        <!-- 加载更多按钮 -->
         <mt-button type="danger" size="large" @click="getMore">加载更多</mt-button>

        
    </div>
</template>

<script>
export default {
    //往自己组件内部，挂载一些私有数据
    data() {
        return {
            pageindex:1 ,//分页的页数
            shoplist:[]
        };
    },
    created(){
        this.getGoodsList()
    },
    methods: {
        getGoodsList(){
            this.$http.get('api/getgoods?pageindex='+this.pageindex).then(result=>{
                if(result.body.status ===0){
                    this.shoplist = this.shoplist.concat(result.body.message);
                }
            })
        },
        getMore(){
            this.pageindex ++;
            this.getGoodsList()
        },
        goNewurl(id){
            
            //使用js的形式进行路由导航
            //注意：this.$route是路由参数对象，this.$router是路由导航对象
            //最简单的方式
            // this.$router.push("/home/commodity/shopinfo/" + id)
            //里面是一个对象时
            // this.$router.push({path:"/home/commodity/shopinfo/",params:{id} })

            // 使用router.js文件中路由命名进行跳转
            this.$router.push({name:'shopinfo',params:{id}})
        }
    },
};
</script>

<style scoped lang="scss">
.goods-list{
    padding: 7px;
    display: flex;
    justify-content: space-between;
    flex-wrap: wrap;
    .goods-item{
        border: 1px solid #ccc;
        box-shadow: 0 0 7px #ccc;
        width: 49%;
        margin-bottom: 7px;
        // 改变因为标题长度不同导致的空白
        display: flex;
        flex-direction: column;
        justify-content: space-between;
        
    }
    img{
        width: 100%;
    }
    .title{
        font-size: 14px;
        text-align: center;
    }
    .info{
        .price{
            .now{
                color: red;
                font-weight: bold;
            }
            .old{
                font-size: 12px;
                text-decoration: line-through;
            }
        }
        p{
            margin: 0;
            padding: 5px;
        }
        .sell{
            color: white;
            font-size: 13px;
            display: flex;
            justify-content: space-between;
            font-weight: bold;
        }
            background-color: #ccc;
            display: block;
        }
}
</style>
